/*
 * Class: CustomerRefundController
 * Created by: Mary Joy Pascual
 * Date Created: 06/25/2014
 * Company: Netsuite
*/
package com.netsuite.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.netsuite.constants.ConstantsCustomerRefund;
import com.netsuite.dao.CustomerRefundDAO;
import com.netsuite.helper.ErrorHelper;
import com.netsuite.logger.LoggerImp;
import com.netsuite.migration.CSV;
import com.netsuite.migration.CSVContent;
import com.netsuite.pojo.CustomerRefundPOJO;

public class CustomerRefundController {
	
	private static String CLASS_NAME = CustomerRefundController.class.getName();
	
	private CustomerRefundDAO cusRefDAO = null;
	
	public CustomerRefundController() throws ErrorHelper{
		cusRefDAO = new CustomerRefundDAO();
	} 
	
	public List<String> getFieldNames() {
		
		LoggerImp.startLog(CLASS_NAME, "getFieldNames()");
		
		List <String> cusRefFieldList = new ArrayList<String>();
		cusRefFieldList.add(ConstantsCustomerRefund.EXT_ID);
		cusRefFieldList.add(ConstantsCustomerRefund.TRAN_ID);
		cusRefFieldList.add(ConstantsCustomerRefund.CUSTOMER);
		cusRefFieldList.add(ConstantsCustomerRefund.ACCOUNT);
		cusRefFieldList.add(ConstantsCustomerRefund.CURRENCY);
		cusRefFieldList.add(ConstantsCustomerRefund.EXC_RATE);
		cusRefFieldList.add(ConstantsCustomerRefund.DATE);
		cusRefFieldList.add(ConstantsCustomerRefund.MEMO);
		cusRefFieldList.add(ConstantsCustomerRefund.REF_METHOD);
		cusRefFieldList.add(ConstantsCustomerRefund.CREDIT_REF_NO);
		cusRefFieldList.add(ConstantsCustomerRefund.CREDIT_PAYMENT);
		cusRefFieldList.add(ConstantsCustomerRefund.DEBIT_REF_NO);
		cusRefFieldList.add(ConstantsCustomerRefund.DEBIT_PAYMENT);
		cusRefFieldList.add(ConstantsCustomerRefund.ID);
		
		LoggerImp.startLog(CLASS_NAME, "getFieldNames()");
		
		return cusRefFieldList;
	}
	
	public String upsertRecord(Map<String, String> titleMap, CSV csv) throws ErrorHelper{
		
		LoggerImp.startLog(CLASS_NAME, "upsertRecord()");
		
		String message = "";
		
		int addedCt = 0;
		int errorCt = 0;
		int lineNo = 0;
		String error = "";
	
		try {

			for (CSVContent csvContent : csv.getCsvContents()){
				CustomerRefundPOJO cusPojo = new CustomerRefundPOJO();
				Iterator it = titleMap.entrySet().iterator();
			    while (it.hasNext()) {
			    	 Map.Entry pairs = (Map.Entry)it.next();
			    	 int index = csv.getCsvTitle().indexOf(pairs.getValue());
			    	 String content =  csvContent.getCsvContents().get(index);
//			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.ID)){
//			    		 cusPojo.setInteralId(content);
//			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.EXT_ID)){
			    		 cusPojo.setExternalId(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.TRAN_ID)){
			    		 cusPojo.setTranId(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.CUSTOMER)){
			    		 cusPojo.setCustomerName(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.ACCOUNT)){
			    		 cusPojo.setAccountName(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.EXC_RATE)){
			    		 cusPojo.setExchangeRate(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.DATE)){
			    		 cusPojo.setDate(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.REF_METHOD)){
			    		 cusPojo.setRefMethod(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.MEMO)){
			    		 cusPojo.setMemo(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.CREDIT_REF_NO)){
			    		 cusPojo.setCreditRef(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.CREDIT_PAYMENT)){
			    		 cusPojo.setCreditPayment(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.DEBIT_REF_NO)){
			    		 cusPojo.setDebitRef(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.DEBIT_PAYMENT)){
			    		 cusPojo.setDebitPayment(content);
			    	 }
			    	 if(pairs.getKey().equals(ConstantsCustomerRefund.CURRENCY)){
			    		 cusPojo.setCurrency(content);
			    	 }
			    }
			    if( cusPojo.getExternalId() == null   ||  cusPojo.getExternalId().length() == 0
			    ||  cusPojo.getCustomerName() == null ||  cusPojo.getCustomerName().length() == 0
			    ||  cusPojo.getAccountName() == null || cusPojo.getAccountName().length() == 0
			    ||  cusPojo.getExchangeRate() == null || cusPojo.getExchangeRate().length() == 0
			    ||  cusPojo.getDate() == null || cusPojo.getDate().length() == 0
			    ||  cusPojo.getRefMethod() == null || cusPojo.getRefMethod().length() == 0){
					  error = error +"---> Err CSV Line #"+String.valueOf(lineNo)+"\n";
					  errorCt++;
				} else {
					  cusRefDAO.upsertCustomerRefund(cusPojo);
					  addedCt++;
				}
			    
				message = "\nTotal No. of Records: "+ csv.getCsvContents().size() + "\n" +
						  "Added/Updated: "+ String.valueOf(addedCt) + " Record/s\n" +
						  "Error Count: "+ String.valueOf(errorCt)   + " Record/s\n" +
						  error;	  
				
		    }

			LoggerImp.infoLog(CLASS_NAME, message);
					
		} catch (Exception e){
			throw new ErrorHelper(CLASS_NAME, e.getMessage());
		}
		
		LoggerImp.endLog(CLASS_NAME, "upsertRecord()");
		
	    return message;
	}

}
